package com.xmut.mall.coupon.controller;

import com.xmut.mall.coupon.annotation.CheckJwt;
import com.xmut.mall.coupon.searchparams.BuyerCouponSearchParam;
import com.xmut.mall.coupon.searchparams.ManagerCouponSearchParam;
import com.xmut.mall.coupon.searchparams.SellerCouponSearchParam;
import com.xmut.mall.coupon.service.CouponService;
import com.xmut.mall.coupon.utils.RespBean;
import com.xmut.mall.coupon.vo.AddCouponVo;
import com.xmut.mall.coupon.vo.AddPlatformCouponVo;
import com.xmut.mall.coupon.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 领取优惠券后减少优惠券数量
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/reduceCouponNum")
    public RespBean reduceCouponNum(@RequestBody Map<String,Object> params){
        return couponService.reduceCouponNum(params);
    }

    /**
     * 获取买家端的优惠券分页数据
     * @param param
     * @return
     */
    @PostMapping("/getBuyerCouponPage")
    public RespBean getBuyerCouponPage(@RequestBody BuyerCouponSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return couponService.getBuyerCouponPage(vo,param);
    }

    /**
     * 用户领取优惠券
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/userReceiveCoupon")
    public RespBean receiveCoupon(@RequestBody Map<String,Object> params){
        return couponService.receiveCoupon(params);
    }

    /**
     * 根据商铺Id获取购物车中可用的优惠券
     * @param storeId
     * @return
     */
    @CheckJwt
    @GetMapping("/getCartShopCoupon/{storeId}")
    public RespBean getCartShopCoupon(@PathVariable Long storeId){
        return couponService.getCartShopCoupon(storeId);
    }

    /**
     * 获取商品详情中的优惠券信息
     * @param skuId
     * @return
     */
    @GetMapping("/getProductDetailCoupon/{skuId}/{cateId}")
    public RespBean getProductDetailCoupon(@PathVariable("skuId") Long skuId,@PathVariable("cateId") Long cateId){
        return couponService.getProductDetailCoupon(skuId,cateId);
    }


    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteCoupon/{id}")
    public RespBean deleteCoupon(@PathVariable Long id){
        return couponService.deleteCoupon(id);
    }

    /**
     * 编辑平台券的内容
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/editPlatformCoupon")
    public  RespBean editPlatformCoupon(@RequestBody AddPlatformCouponVo vo){
        return couponService.editPlatformCoupon(vo);
    }

    /**
     * 根据ID获取平台券的详情
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getPlatformCouponDetail/{id}")
    public RespBean getPlatformCouponDetail(@PathVariable Long id){
        return couponService.getPlatformCouponDetail(id);
    }

    /**
     * 添加平台优惠券(平台券)
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/AddPlatformCoupon")
    public RespBean AddPlatformCoupon(@RequestBody AddPlatformCouponVo vo){
        return couponService.AddPlatformCoupon(vo);
    }

    /**
     * 获取商城管理端的优惠券分页数据 获取平台券
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerCouponPage")
    public RespBean getManagerCouponPage(@RequestBody ManagerCouponSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return couponService.getManagerCouponPage(vo,param);
    }

    /**
     * 根据传入的Map参数获取
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerCouponList")
    public RespBean getManagerCouponList(@RequestBody Map<String,Object> params){
        return couponService.getManagerCouponList(params);
    }

    /**
     * 开启优惠券活动
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/openCoupon")
    public RespBean openCoupon(@RequestBody Map<String,Object> params){
        return couponService.openCoupon(params);
    }

    /**
     * 关闭当前优惠券活动
     * @param id
     * @return
     */
    @CheckJwt
    @PutMapping("/closeCoupon/{id}")
    public RespBean closeCoupon(@PathVariable Long id){
        return couponService.closeCoupon(id);
    }

    /**
     * 编辑优惠券
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/editCoupon")
    public RespBean editCoupon(@RequestBody AddCouponVo vo){
        return couponService.editCoupon(vo);
    }

    /**
     * 根据ID获取优惠券详情
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getCouponDetail/{id}")
    public RespBean getCouponDetail(@PathVariable Long id){
        return couponService.getCouponDetail(id);
    }

    /**
     * 添加优惠券
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/addCoupon")
    public RespBean addCoupon(@RequestBody AddCouponVo vo) {
        return couponService.addCoupon(vo);
    }

    /**
     * 获取卖家端优惠券的分页数据
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getSellerCouponPage")
    public RespBean getSellerCouponPage(@RequestBody SellerCouponSearchParam param) {
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param, vo);
        return couponService.getSellerCouponPage(vo,param);
    }

    /**
     * 卖家端获取优惠券列表
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/sellerGetCouponList")
    public RespBean sellerGetCouponList(@RequestBody Map<String,Object> params){
        return couponService.sellerGetCouponList(params);
    }

    @GetMapping("/getCoupon/{id}")
    public RespBean getCoupon(@PathVariable Long id){
        return RespBean.success("获取成功",couponService.getById(id));
    }
}
